package com.spark.gaea.security.backend;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.spark.gaea.security.domain.SecurityUser;
import com.spark.gaea.utils.HttpUtil;
import com.spark.gaea.utils.exception.GaeaRuntimeException;
import com.spark.gaea.utils.json.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class HttpSecurity implements SecurityBackend {
	@Autowired
	Environment env;

	private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

	private HttpUtil http;
	private ObjectMapper mapper;
	private String mdmApiUrl;

	public HttpSecurity() {
		this.http = new HttpUtil();
		this.mapper = JsonUtil.getMapper();
	}

	@PostConstruct
	public void init() {
		this.mdmApiUrl = HttpUtil.getAppUrl(this.env.getProperty("mdm_url")) + "api/";
	}

	@Override
	public String getUserId(final String username) {
		final Map<String, String> params = Maps.newHashMap();
		params.put("username", username);
		params.put("command", "getSecurityUserPk");
		try {
			LOGGER.debug(this.mdmApiUrl + "getSecurityUserPk");
			final String response = this.http.post(this.mdmApiUrl + "getSecurityUserPk", params);
			return this.mapper.readValue(response, String.class);
		} catch (IOException e) {
			throw new GaeaRuntimeException("远程连接无效", e);
		}
	}

	@Override
	public SecurityUser getUser(final String username) {
		final Map<String, String> params = Maps.newHashMap();
		params.put("username", username);
		params.put("command", "getSecurityUser");
		try {
			LOGGER.debug(this.mdmApiUrl + "getSecurityUser");
			final String response = this.http.post(this.mdmApiUrl + "getSecurityUser", params);
			return this.mapper.readValue(response, SecurityUser.class);
		} catch (IOException e) {
			throw new GaeaRuntimeException("远程连接无效", e);
		}
	}

	public Set<String> getUserRoles(final String userId) {
		final Map<String, String> params = Maps.newHashMap();
		params.put("id", userId);
		params.put("command", "getSecurityRoles");
		try {
			LOGGER.debug(this.mdmApiUrl + "getSecurityRoles");
			final String response = this.http.post(this.mdmApiUrl + "getSecurityRoles", params);
			return this.mapper.readValue(response, HashSet.class);
		} catch (IOException e) {
			throw new GaeaRuntimeException("远程连接无效", e);
		}
	}

	public Set<String> getUserPermissions(final String userId) {
		final Map<String, String> params = Maps.newHashMap();
		params.put("id", userId);
		params.put("command", "getSecurityPermissions");
		try {
			LOGGER.debug(this.mdmApiUrl + "getSecurityPermissions");
			final String response = this.http.post(this.mdmApiUrl + "getSecurityPermissions", params);
			return this.mapper.readValue(response, HashSet.class);
		} catch (IOException e) {
			throw new GaeaRuntimeException("远程连接无效", e);
		}
	}

	@Override
	public Set<String> getUserPermissionsByMisid(final String userId, final String misId) {
		final Map<String, String> params = Maps.newHashMap();
		params.put("id", userId);
		params.put("misId", misId);
		params.put("command", "getSecurityPermissions");
		try {
			LOGGER.debug(this.mdmApiUrl + "getSecurityPermissions");
			final String response = this.http.post(this.mdmApiUrl + "getSecurityPermissions", params);
			return this.mapper.readValue(response, HashSet.class);
		} catch (IOException e) {
			throw new GaeaRuntimeException("远程连接无效", e);
		}
	}
}
